package com.pubinfo.passbook.data.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pubinfo.passbook.common.utils.SessionCookieUtils;
import com.pubinfo.passbook.common.entity.TSystemUser;
import com.pubinfo.passbook.common.model.dto.system.UserParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.master.TSystemUserService;
import com.pubinfo.passbook.shiro.token.JwtUtil;
import com.pubinfo.passbook.shiro.utils.Pbkdf2Sha256;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping(value = "user")
@Api(tags = "用户登录")
@Slf4j
public class LoginController {
  @Resource
  TSystemUserService userService;
  
  @PostMapping(value = "login")
  @ApiOperation("登录")
  public JsonResult<String> debugLogin(@RequestBody UserParam userParam, HttpServletResponse response) {
    log.info("登录输入参数为: username:{},password:{}", userParam.getUsername(), "***");
    String username = userParam.getUsername();
    String password = userParam.getPassword();
    //判断用户是否登录成功
    if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
      return JsonResult.error("请输入用户名和密码!");
    } else {
      
      UsernamePasswordToken token = null;
      QueryWrapper<TSystemUser> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("username", username);
      List<TSystemUser> userList = userService.list(queryWrapper);
      
      if (CollectionUtils.isEmpty(userList)) {
        return JsonResult.error("该用户并不存在!");
      }
      TSystemUser user = userList.get(0);
      //密码校验
      if (Pbkdf2Sha256.verificationHash(password, user.getPassword(), user.getSalt())) {
        //1.封装用户信息
        
        token = new UsernamePasswordToken(username, password);
        //2.提交用户信息
        Subject subject = SecurityUtils.getSubject();
        subject.getSession().setTimeout(1000 * 60 * 60 * 24);
        token.setRememberMe(true);
        subject.login(token);
        return JsonResult.success(subject.getSession().getId().toString());
//        String sign=JwtUtil.sign(user.getUsername(), user.getId(),JwtUtil.SECRET);
//        //String sign = JwtUtil.sign(user.getUsername(), user.getId(),  JwtUtil.SECRET);
//        Cookie mycookie = new Cookie("x-token", sign);
//        mycookie.setPath("/");
//        //cookie有效时间30分钟
//        mycookie.setMaxAge(60 * 60 * 1000);
//        //将cookie返回给页面
//        response.addCookie(mycookie);
//        return JsonResult.success(sign);
      }
      //返回token
      
    }
    
    return JsonResult.success("失败");
  }
  
  @PutMapping("/logout")
  @ApiOperation("退出")
  public JsonResult<String> logout() {
    SessionCookieUtils.clearCookieAll();
    SecurityUtils.getSubject().logout();
    return JsonResult.success("logout成功");
  }
}
